package com.xsdl.singleton;

import java.util.concurrent.atomic.AtomicLong;

/*
 * 静态内部类实现单例,双重检测的简化版
 * 只有当真正调用 getInstance 方法时, InnerClass 类才会被加载
 */
public class IDGenerator {

    private final AtomicLong id = new AtomicLong(1L);

    private IDGenerator() {

    }

    private static class InnerClass {
        private static final IDGenerator instance = new IDGenerator();
    }

    public static IDGenerator getInstance() {
        return InnerClass.instance;
    }

    public Long getNextId() {
        return id.getAndIncrement();
    }

}
